package cn.white.ymc.mvvmmaster.data;

import android.annotation.SuppressLint;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.ViewModel;
import android.content.Context;

import cn.white.ymc.mvvmmaster.data.bean.UserInfo;
import cn.white.ymc.mvvmmaster.data.local.LocalUserDataSource;
import cn.white.ymc.mvvmmaster.data.util.NetworkUtils;
import cn.white.ymc.mvvmmaster.data.util.UserInfoRepository;

/**
 *  userInfo view Model
 *
 * @packageName: cn.white.ymc.mvvmdemo.data
 * @fileName: UserInfoViewModel
 * @date: 2018/11/5  17:28
 * @author: ymc
 * @QQ:745612618
 */

public class UserInfoViewModel extends ViewModel {

    private UserInfoRepository userInfoRepository = UserInfoRepository.getInstance();

    private LiveData<UserInfo> user;

    @SuppressLint("StaticFieldLeak")
    private Context context;

    public void init(Context context) {
        this.context = context.getApplicationContext();
    }

    public LiveData<UserInfo> getUser(String username) {
        if(NetworkUtils.INSTANCE.isConnected(context)){
            user = userInfoRepository.getUser(username);
        }else{
            user =  LocalUserDataSource.getInstance().queryUserByUsername(username);
        }

        return user;
    }



}
